// pages/addcookbook/addcookbook.js
const GetPeriod = require("../../utils/getperiod.js");
var util = require("../../utils/util.js");
const MONTHS = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'June.', 'July.', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.'];
const qiniuUploader = require("../../utils/qiniuUploader");
var utils = require('../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    showflag: true,
    content: null,
    dateStart: null,
    dateEnd: null,
    date2: null,
    date3: null,
    date4: null,
    date5: null,
    date6: null,
    now: null,
    dates: null,
    num: 0,
    week: null,
    year: null,
    type: 1,
    leftDay: "2020-08-06",
    rightDay: "2020-08-06",
    cookbookaddrole: 0,
    status: null,
    photo: "../../images/icon_paizhao.png",
    photoList: [],
    qiniuToken: "",
    saverurl: "",
    copy: false,
    dishes: [],
    mealset: [],
    data: [],
    copy: false,
    fabuflag:false
  },

  getweekString: function (date1) {
    var Date1 = new Date(date1)
    var y = Date1.getFullYear()
    var Date2 = new Date(y + "/1/1")
    console.log(Date2)
    var dayOfWeek = Date2.getDay();
    //如果把周日算在一周的最后一天，请加上下面这句
    dayOfWeek = dayOfWeek == 0 ? 7 : dayOfWeek
    //如果把周日算在一周的第一天，请删除上面这句
    var num = (Date1 - Date2) / 1000 / 3600 / 24
    var whichWeek = Math.ceil((num + dayOfWeek) / 7)
    this.setData({
      week: whichWeek - 1
    })

    console.log("您查询的日期在本年度的第" + whichWeek + "周！")
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;
     this.setData({
      num:parseInt(options.num)
     })
    that.getWeekStartDate(this.data.num)
    this.getData()
    /**
     * 获取当前设备的宽高
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }

    });

    this.getQiniuToken()
  },


  //获取本周的开始日期
  getWeekStartDate(numDay) {
    let that = this;
    this.now = new Date();
    this.nowYear = this.now.getYear(); //当前年 
    this.nowMonth = this.now.getMonth(); //当前月 
    this.nowDay = this.now.getDate(); //当前日 
    this.nowDayOfWeek = this.now.getDay(); //今天是本周的第几天 
    this.nowYear += (this.nowYear < 2000) ? 1900 : 0;
    var a = new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek + 1 + numDay);
    let dateStart =""
    let date2 =""
    let date3 =""
    let date4 =""
    let date5 =""
    let date6 =""
    let dateEnd =""
   if(this.nowDayOfWeek==0){

    dateStart = GetPeriod.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek + 1 + numDay-7));
    date2 = GetPeriod.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek + 2 + numDay-7));
    date3 = GetPeriod.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek + 3 + numDay-7));
    date4 = GetPeriod.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek + 4 + numDay-7));
    date5 = GetPeriod.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek + 5 + numDay-7));
    date6 = GetPeriod.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek + 6 + numDay-7));
    dateEnd = GetPeriod.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek + 7 + numDay-7));
   }
   else{
    dateStart = GetPeriod.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek + 1 + numDay));
    date2 = GetPeriod.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek + 2 + numDay));
    date3 = GetPeriod.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek + 3 + numDay));
    date4 = GetPeriod.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek + 4 + numDay));
    date5 = GetPeriod.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek + 5 + numDay));
    date6 = GetPeriod.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek + 6 + numDay));
    dateEnd = GetPeriod.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek + 7 + numDay));
   }





    // console.log(dateStart)
    // 获取今天日期
    let now = GetPeriod.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay));
    now = now.replace(/-/g, "/");
    now = now.substring(5);
    this.setData({
      dateStart: dateStart,
      dateEnd: dateEnd,
      date2: date2,
      date3: date3,
      date4: date4,
      date5: date5,
      date6: date6,
      now: now,
      dates: now,
      year: a.getFullYear()
    })
    this.getweekString(dateStart);
    // 初始化数据(历史纪录)
    var timestamp = Date.parse(new Date(this.data.dateStart));
    timestamp = timestamp / 1000;
    // console.log(timestamp);
    that.setData({
      timestamp: timestamp,
    })


  },


  sendImge: function (iscopy, data1, week) {
    wx.showNavigationBarLoading() //在当前页面显示导航条加载动画
    wx.showLoading({ //显示 loading 提示框
      title: "处理中...",
    })
    for (var i = 0; i < this.data.photoList.length; i++) {
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var _this = this
      var filepath = this.data.photoList[i]
      // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
      qiniuUploader.upload(filepath, res => {
        console.log(res)
        var url = res.imageURL
        data1[0].mdata[0].attachmentID = res.fileId
        data1[0].mdata[0].attachmentUrl = url

        _this.sendtextData(iscopy, data1, week)
      }, (error) => {
        wx.hideNavigationBarLoading()
        wx.hideLoading()
        wx.showToast({
          title: "保存失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
        _this.setData({
          fabuflag:false
        })
        return;
      }, {
        key: "jygy/" + "photo/" + app.globalData.schoolId + "/" + app.globalData.classid + "/" + timestamp + "/" + utils.formatDate(new Date()) + utils.wxuuid() + ".jpg",
        region: 'NCN',
        uptoken: _this.data.qiniuToken,
        uploadURL: 'https://up-z1.qbox.me',
        domain: app.globalData.qiniurUrl,
      })
    }
  },



  getQiniuToken: function (e) {
    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan
    }
    wx.request({
      url: app.globalData.serverQiNiuUrl,
      data: {
        "data": JSON.stringify(data)
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          that.setData({
            qiniuToken: res.data.data.token
          })
        } else {

        }
      },
      fail: function (res) {

      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },


  photoClick: function (e) {
    console.log(e);
    var that = this
    wx.chooseImage({
      count: 1, // 默认最多上传1张
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        var tempFilePaths = res.tempFilePaths; // 返回选定照片的本地路径列表 
        that.setData({
          photoList: tempFilePaths,
          photo: tempFilePaths[0]
        })
        console.log(that.data.photoList)
      }
    })

    console.log("点击");

  },


  //  tab切换逻辑
  swichNav: function (e) {
    if (this.data.currentTab == e.target.dataset.current) {
      return false;
    } else {
      var that = this;
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },

  bindChange: function (e) {

    var that = this;
    that.setData({
      currentTab: e.detail.current
    });

  },


  addzhouyi: function (e) {
    var index = e.currentTarget.dataset.index;
    var obj = {
      meal_pic_id: 0,
      meal_name: "",
      attachmentUrl: ""
    }
    var mealset = this.data.mealset
    var list = []
    if (mealset[index].zhouyilist != null) {
      list = mealset[index].zhouyilist
    }
    list.push(obj)

    this.setData({
      mealset: mealset
    })
    this.getHeight()
  },

  delzhouyi: function (e) {
    console.log('当前下标---', e.target.dataset.imgindex)
    console.log('当前父级下标---', e.target.dataset.parent)
    var indexp = e.target.dataset.parent;
    var indexc = e.target.dataset.imgindex;
    var mealset = this.data.mealset
    var list = mealset[indexp].zhouyilist
    list.splice(indexc, 1);
    mealset.list = list
    this.setData({
      mealset: mealset
    })
    this.getHeight()
  },

  zhouyiBtnClick: function (e) {
    console.log('当前下标---', e.target.dataset.imgindex)
    console.log('当前父级下标---', e.target.dataset.parent)
    var indexp = e.target.dataset.parent;
    var indexc = e.target.dataset.imgindex;
    var disheststr = JSON.stringify(this.data.dishes)
    wx.navigateTo({
      url: '../choosedishes/choosedishes?disheststr=' + disheststr + "&indexp=" + indexp + "&indexc=" + indexc + "&weekday=" + 1,
    })
  },

  zhouerBtnClick: function (e) {
    console.log('当前下标---', e.target.dataset.imgindex)
    console.log('当前父级下标---', e.target.dataset.parent)
    var indexp = e.target.dataset.parent;
    var indexc = e.target.dataset.imgindex;
    var disheststr = JSON.stringify(this.data.dishes)
    wx.navigateTo({
      url: '../choosedishes/choosedishes?disheststr=' + disheststr + "&indexp=" + indexp + "&indexc=" + indexc + "&weekday=" + 2,
    })
  },

  zhousanBtnClick: function (e) {
    console.log('当前下标---', e.target.dataset.imgindex)
    console.log('当前父级下标---', e.target.dataset.parent)
    var indexp = e.target.dataset.parent;
    var indexc = e.target.dataset.imgindex;
    var disheststr = JSON.stringify(this.data.dishes)
    wx.navigateTo({
      url: '../choosedishes/choosedishes?disheststr=' + disheststr + "&indexp=" + indexp + "&indexc=" + indexc + "&weekday=" + 3,
    })
  },

  zhousiBtnClick: function (e) {
    console.log('当前下标---', e.target.dataset.imgindex)
    console.log('当前父级下标---', e.target.dataset.parent)
    var indexp = e.target.dataset.parent;
    var indexc = e.target.dataset.imgindex;
    var disheststr = JSON.stringify(this.data.dishes)
    wx.navigateTo({
      url: '../choosedishes/choosedishes?disheststr=' + disheststr + "&indexp=" + indexp + "&indexc=" + indexc + "&weekday=" + 4,
    })
  },

  zhouwuBtnClick: function (e) {
    console.log('当前下标---', e.target.dataset.imgindex)
    console.log('当前父级下标---', e.target.dataset.parent)
    var indexp = e.target.dataset.parent;
    var indexc = e.target.dataset.imgindex;
    var disheststr = JSON.stringify(this.data.dishes)
    wx.navigateTo({
      url: '../choosedishes/choosedishes?disheststr=' + disheststr + "&indexp=" + indexp + "&indexc=" + indexc + "&weekday=" + 5,
    })
  },

  zhouliuBtnClick: function (e) {
    console.log('当前下标---', e.target.dataset.imgindex)
    console.log('当前父级下标---', e.target.dataset.parent)
    var indexp = e.target.dataset.parent;
    var indexc = e.target.dataset.imgindex;
    var disheststr = JSON.stringify(this.data.dishes)
    wx.navigateTo({
      url: '../choosedishes/choosedishes?disheststr=' + disheststr + "&indexp=" + indexp + "&indexc=" + indexc + "&weekday=" + 6,
    })
  },

  zhouqiBtnClick: function (e) {
    console.log('当前下标---', e.target.dataset.imgindex)
    console.log('当前父级下标---', e.target.dataset.parent)
    var indexp = e.target.dataset.parent;
    var indexc = e.target.dataset.imgindex;
    var disheststr = JSON.stringify(this.data.dishes)
    wx.navigateTo({
      url: '../choosedishes/choosedishes?disheststr=' + disheststr + "&indexp=" + indexp + "&indexc=" + indexc + "&weekday=" + 7,
    })

  },





  weekplanezhouer: function (e) {
    var index = e.currentTarget.dataset.index;
    var list = this.data.zhouerlist
    list[index].meal_name = e.detail.value
    this.setData({
      zhouerlist: list
    })

  },
  addzhouer: function (e) {
    var index = e.currentTarget.dataset.index;
    var obj = {
      meal_pic_id: 0,
      meal_name: "",
      attachmentUrl: ""
    }
    var mealset = this.data.mealset
    var list = []
    if (mealset[index].zhouerlist != null) {
      list = mealset[index].zhouerlist
    }
    list.push(obj)

    this.setData({
      mealset: mealset
    })
    this.getHeight()

  },

  delzhouer: function (e) {
    console.log('当前下标---', e.target.dataset.imgindex)
    console.log('当前父级下标---', e.target.dataset.parent)
    var indexp = e.target.dataset.parent;
    var indexc = e.target.dataset.imgindex;
    var mealset = this.data.mealset
    var list = mealset[indexp].zhouerlist
    list.splice(indexc, 1);
    mealset.list = list
    this.setData({
      mealset: mealset
    })
    this.getHeight()
  },






  weekplanezhousan: function (e) {
    var index = e.currentTarget.dataset.index;
    var list = this.data.zhousanlist
    list[index].meal_name = e.detail.value
    this.setData({
      zhousanlist: list
    })

  },
  addzhousan: function (e) {
    var index = e.currentTarget.dataset.index;
    var obj = {
      meal_pic_id: 0,
      meal_name: "",
      attachmentUrl: ""
    }
    var mealset = this.data.mealset
    var list = []
    if (mealset[index].zhousanlist != null) {
      list = mealset[index].zhousanlist
    }
    list.push(obj)

    this.setData({
      mealset: mealset
    })
    this.getHeight()

  },

  delzhousan: function (e) {
    console.log('当前下标---', e.target.dataset.imgindex)
    console.log('当前父级下标---', e.target.dataset.parent)
    var indexp = e.target.dataset.parent;
    var indexc = e.target.dataset.imgindex;
    var mealset = this.data.mealset
    var list = mealset[indexp].zhousanlist
    list.splice(indexc, 1);
    mealset.list = list
    this.setData({
      mealset: mealset
    })
    this.getHeight()
  },









  weekplanezhousi: function (e) {
    var index = e.currentTarget.dataset.index;
    var list = this.data.zhousilist
    list[index].meal_name = e.detail.value
    this.setData({
      zhousilist: list
    })

  },
  addzhousi: function (e) {
    var index = e.currentTarget.dataset.index;
    var obj = {
      meal_pic_id: 0,
      meal_name: "",
      attachmentUrl: ""
    }
    var mealset = this.data.mealset
    var list = []
    if (mealset[index].zhousilist != null) {
      list = mealset[index].zhousilist
    }
    list.push(obj)

    this.setData({
      mealset: mealset
    })
    this.getHeight()

  },

  delzhousi: function (e) {
    console.log('当前下标---', e.target.dataset.imgindex)
    console.log('当前父级下标---', e.target.dataset.parent)
    var indexp = e.target.dataset.parent;
    var indexc = e.target.dataset.imgindex;
    var mealset = this.data.mealset
    var list = mealset[indexp].zhousilist
    list.splice(indexc, 1);
    mealset.list = list
    this.setData({
      mealset: mealset
    })
    this.getHeight()
  },









  weekplanezhouwu: function (e) {
    var index = e.currentTarget.dataset.index;
    var list = this.data.zhouwulist
    list[index].meal_name = e.detail.value
    this.setData({
      zhouwulist: list
    })

  },
  addzhouwu: function (e) {
    var index = e.currentTarget.dataset.index;
    var obj = {
      meal_pic_id: 0,
      meal_name: "",
      attachmentUrl: ""
    }
    var mealset = this.data.mealset
    var list = []
    if (mealset[index].zhouwulist != null) {
      list = mealset[index].zhouwulist
    }
    list.push(obj)

    this.setData({
      mealset: mealset
    })
    this.getHeight()
  },

  delzhouwu: function (e) {
    console.log('当前下标---', e.target.dataset.imgindex)
    console.log('当前父级下标---', e.target.dataset.parent)
    var indexp = e.target.dataset.parent;
    var indexc = e.target.dataset.imgindex;
    var mealset = this.data.mealset
    var list = mealset[indexp].zhouwulist
    list.splice(indexc, 1);
    mealset.list = list
    this.setData({
      mealset: mealset
    })
    this.getHeight()
  },








  weekplanezhouliu: function (e) {
    var index = e.currentTarget.dataset.index;
    var list = this.data.zhouliulist
    list[index].meal_name = e.detail.value
    this.setData({
      zhouliulist: list
    })

  },
  addzhouliu: function (e) {
    var index = e.currentTarget.dataset.index;
    var obj = {
      meal_pic_id: 0,
      meal_name: "",
      attachmentUrl: ""
    }
    var mealset = this.data.mealset
    var list = []
    if (mealset[index].zhouliulist != null) {
      list = mealset[index].zhouliulist
    }
    list.push(obj)

    this.setData({
      mealset: mealset
    })
    this.getHeight()
  },

  delzhouliu: function (e) {
    console.log('当前下标---', e.target.dataset.imgindex)
    console.log('当前父级下标---', e.target.dataset.parent)
    var indexp = e.target.dataset.parent;
    var indexc = e.target.dataset.imgindex;
    var mealset = this.data.mealset
    var list = mealset[indexp].zhouliulist
    list.splice(indexc, 1);
    mealset.list = list
    this.setData({
      mealset: mealset
    })
    this.getHeight()
  },






  weekplanezhouqi: function (e) {
    var index = e.currentTarget.dataset.index;
    var list = this.data.zhouqilist
    list[index].meal_name = e.detail.value
    this.setData({
      zhouqilist: list
    })

  },
  addzhouqi: function (e) {
    var index = e.currentTarget.dataset.index;
    var obj = {
      meal_pic_id: 0,
      meal_name: "",
      attachmentUrl: ""
    }
    var mealset = this.data.mealset
    var list = []
    if (mealset[index].zhouqilist != null) {
      list = mealset[index].zhouqilist
    }
    list.push(obj)

    this.setData({
      mealset: mealset
    })
    this.getHeight()
  },

  delzhouqi: function (e) {
    console.log('当前下标---', e.target.dataset.imgindex)
    console.log('当前父级下标---', e.target.dataset.parent)
    var indexp = e.target.dataset.parent;
    var indexc = e.target.dataset.imgindex;
    var mealset = this.data.mealset
    var list = mealset[indexp].zhouqilist
    list.splice(indexc, 1);
    mealset.list = list
    this.setData({
      mealset: mealset
    })
    this.getHeight()
  },



  //切换到图片版
  qiehuanpicClick: function (e) {
    this.setData({
      type: 2
    })
  },

  //切换到文字版
  qiehuantextClick: function (e) {
    this.setData({
      type: 1
    })
  },

  //复制到下一周
  copyClick: function (e) {
    if(this.data.fabuflag==true){
      return
    }
    this.getWeekStartDate(this.data.num);
    this.savedata(true, this.data.week)

  },



  savedata: function (iscopy, week) {

    if (this.data.type == 1) {

      var data = []

      //周一数据
      var dateobj1 = {
        "date": this.data.dateStart,
        "week": 1,
        "mdata": []
      }


      for (var i = 0; i < this.data.mealset.length; i++) {

        var mealObj = this.data.mealset[i]
        var dishObj = this.data.mealset[i].zhouyilist
        for (var j = 0; j < dishObj.length; j++) {
          if (dishObj[j].meal_name == null || dishObj[j].meal_name == "" || dishObj[j].meal_name.length == 0) {
            wx.showToast({
              title: "请选择食品",
              icon: 'none',
              duration: 1000,
              mask: true
            })
            return;
          }
        }
        var obj = {}
        obj.mid = mealObj.mid
        obj.name = mealObj.name
        obj.othername = mealObj.othername
        obj.begin_time = mealObj.begin_time
        obj.over_time = mealObj.over_time
        obj.show_time = mealObj.show_time
        obj.meal_list = dishObj
        dateobj1.mdata.push(obj)

      }
      data.push(dateobj1)






      //周二数据
      var dateobj2 = {
        "date": this.data.date2,
        "week": 2,
        "mdata": []
      }

      for (var i = 0; i < this.data.mealset.length; i++) {

        var mealObj = this.data.mealset[i]
        var dishObj = this.data.mealset[i].zhouerlist
        for (var j = 0; j < dishObj.length; j++) {
          if (dishObj[j].meal_name == null || dishObj[j].meal_name == "" || dishObj[j].meal_name.length == 0) {
            wx.showToast({
              title: "请选择食品",
              icon: 'none',
              duration: 1000,
              mask: true
            })
            return;
          }
        }
        var obj = {}
        obj.mid = mealObj.mid
        obj.name = mealObj.name
        obj.othername = mealObj.othername
        obj.begin_time = mealObj.begin_time
        obj.over_time = mealObj.over_time
        obj.show_time = mealObj.show_time
        obj.meal_list = dishObj
        dateobj2.mdata.push(obj)

      }
      data.push(dateobj2)



      //周三数据
      var dateobj3 = {
        "date": this.data.date3,
        "week": 3,
        "mdata": []
      }

      for (var i = 0; i < this.data.mealset.length; i++) {

        var mealObj = this.data.mealset[i]
        var dishObj = this.data.mealset[i].zhousanlist
        for (var j = 0; j < dishObj.length; j++) {
          if (dishObj[j].meal_name == null || dishObj[j].meal_name == "" || dishObj[j].meal_name.length == 0) {
            wx.showToast({
              title: "请选择食品",
              icon: 'none',
              duration: 1000,
              mask: true
            })
            return;
          }
        }

        var obj = {}
        obj.mid = mealObj.mid
        obj.name = mealObj.name
        obj.othername = mealObj.othername
        obj.begin_time = mealObj.begin_time
        obj.over_time = mealObj.over_time
        obj.show_time = mealObj.show_time
        obj.meal_list = dishObj
        dateobj3.mdata.push(obj)

      }
      data.push(dateobj3)



      //周四数据
      var dateobj4 = {
        "date": this.data.date4,
        "week": 4,
        "mdata": []
      }

      for (var i = 0; i < this.data.mealset.length; i++) {

        var mealObj = this.data.mealset[i]
        var dishObj = this.data.mealset[i].zhousilist
        for (var j = 0; j < dishObj.length; j++) {
          if (dishObj[j].meal_name == null || dishObj[j].meal_name == "" || dishObj[j].meal_name.length == 0) {
            wx.showToast({
              title: "请选择食品",
              icon: 'none',
              duration: 1000,
              mask: true
            })
            return;
          }
        }
        var obj = {}
        obj.mid = mealObj.mid
        obj.name = mealObj.name
        obj.othername = mealObj.othername
        obj.begin_time = mealObj.begin_time
        obj.over_time = mealObj.over_time
        obj.show_time = mealObj.show_time
        obj.meal_list = dishObj
        dateobj4.mdata.push(obj)

      }
      data.push(dateobj4)



      //周五数据
      var dateobj5 = {
        "date": this.data.date5,
        "week": 5,
        "mdata": []
      }
      for (var i = 0; i < this.data.mealset.length; i++) {

        var mealObj = this.data.mealset[i]
        var dishObj = this.data.mealset[i].zhouwulist
        for (var j = 0; j < dishObj.length; j++) {
          if (dishObj[j].meal_name == null || dishObj[j].meal_name == "" || dishObj[j].meal_name.length == 0) {
            wx.showToast({
              title: "请选择食品",
              icon: 'none',
              duration: 1000,
              mask: true
            })
            return;
          }
        }
        var obj = {}
        obj.mid = mealObj.mid
        obj.name = mealObj.name
        obj.othername = mealObj.othername
        obj.begin_time = mealObj.begin_time
        obj.over_time = mealObj.over_time
        obj.show_time = mealObj.show_time
        obj.meal_list = dishObj
        dateobj5.mdata.push(obj)

      }
      data.push(dateobj5)



      //周六数据
      var dateobj6 = {
        "date": this.data.date6,
        "week": 6,
        "mdata": []
      }

      for (var i = 0; i < this.data.mealset.length; i++) {

        var mealObj = this.data.mealset[i]
        var dishObj = this.data.mealset[i].zhouliulist
        for (var j = 0; j < dishObj.length; j++) {
          if (dishObj[j].meal_name == null || dishObj[j].meal_name == "" || dishObj[j].meal_name.length == 0) {
            wx.showToast({
              title: "请选择食品",
              icon: 'none',
              duration: 1000,
              mask: true
            })
            return;
          }
        }
        var obj = {}
        obj.mid = mealObj.mid
        obj.name = mealObj.name
        obj.othername = mealObj.othername
        obj.begin_time = mealObj.begin_time
        obj.over_time = mealObj.over_time
        obj.show_time = mealObj.show_time
        obj.meal_list = dishObj
        dateobj6.mdata.push(obj)

      }
      data.push(dateobj6)


      //周日数据
      var dateobj7 = {
        "date": this.data.dateEnd,
        "week": 7,
        "mdata": []
      }
      for (var i = 0; i < this.data.mealset.length; i++) {

        var mealObj = this.data.mealset[i]
        var dishObj = this.data.mealset[i].zhouqilist
        for (var j = 0; j < dishObj.length; j++) {
          if (dishObj[j].meal_name == null || dishObj[j].meal_name == "" || dishObj[j].meal_name.length == 0) {
            wx.showToast({
              title: "请选择食品",
              icon: 'none',
              duration: 1000,
              mask: true
            })
            return;
          }
        }
        var obj = {}
        obj.mid = mealObj.mid
        obj.name = mealObj.name
        obj.othername = mealObj.othername
        obj.begin_time = mealObj.begin_time
        obj.over_time = mealObj.over_time
        obj.show_time = mealObj.show_time
        obj.meal_list = dishObj
        dateobj7.mdata.push(obj)

      }
      data.push(dateobj7)

      this.setData({
        fabuflag:true
      })
      wx.showNavigationBarLoading() //在当前页面显示导航条加载动画
      wx.showLoading({ //显示 loading 提示框
        title: "正在保存...",
      })
      this.sendtextData(iscopy, data, week)


    } else {

      if (this.data.photo == '../../images/icon_paizhao.png' || this.data.photo == null || this.data.photo.length == 0 || this.data.photo == this.data.saverurl) {
        wx.showToast({
          title: "请选择照片",
          icon: 'none',
          duration: 1000,
          mask: true
        })
        return;
      }


      var data = []
      var dateobj1 = {
        "date": this.data.dateStart,
        "week": 1,
        "mdata": []
      }
      var mdata = {
        "sType": 1,
        "attachmentID": 0,
        "attachmentUrl": ""
      }
      dateobj1.mdata.push(mdata)
      data.push(dateobj1)
      this.setData({
        fabuflag:true
      })
      this.sendImge(iscopy, data, week)
    }
  },






  //保存
  saveweekplan: function (e) {
    if(this.data.fabuflag==true){
      return
    }
    this.getWeekStartDate(this.data.num);
    this.savedata(false, this.data.week)


  },

  sendtextData: function (iscopy, data1, week) {
    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "weekOrder": week,
      "cookbookType": this.data.type,
      "year": this.data.year,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": "",
      "data": data1
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "send_cookbook"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          wx.showToast({
            title: "保存成功",
            icon: 'none',
            duration: 1000,
            mask: true
          })
          if (!iscopy) {
            wx.navigateBack({
              url: '../cookbook/cookbook'
            })
          } else {
            that.data.num = that.data.num + 7;
            that.getWeekStartDate(that.data.num);
            that.savedata(false, that.data.week)
          }

        } else {
          wx.showToast({
            title: "保存失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
          that.setData({
            fabuflag:false
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: "保存失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
        that.setData({
          fabuflag:false
        })

      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },

  getHeight: function () {
    var rows = 0;
    var length = 0

    for (var i = 0; i < this.data.mealset.length; i++) {
      length += this.data.mealset[i].zhouyilist.length
    }

    if (length > rows) {
      rows = length
    }

    length = 0
    for (var i = 0; i < this.data.mealset.length; i++) {
      length += this.data.mealset[i].zhouerlist.length
    }

    if (length > rows) {
      rows = length
    }

    length = 0
    for (var i = 0; i < this.data.mealset.length; i++) {
      length += this.data.mealset[i].zhousanlist.length
    }

    if (length > rows) {
      rows = length
    }

    length = 0
    for (var i = 0; i < this.data.mealset.length; i++) {
      length += this.data.mealset[i].zhousilist.length
    }

    if (length > rows) {
      rows = length
    }

    length = 0
    for (var i = 0; i < this.data.mealset.length; i++) {
      length += this.data.mealset[i].zhouwulist.length
    }

    if (length > rows) {
      rows = length
    }

    length = 0
    for (var i = 0; i < this.data.mealset.length; i++) {
      length += this.data.mealset[i].zhouliulist.length
    }

    if (length > rows) {
      rows = length
    }

    length = 0
    for (var i = 0; i < this.data.mealset.length; i++) {
      length += this.data.mealset[i].zhouqilist.length
    }

    if (length > rows) {
      rows = length
    }

    if (rows > 0) {
      this.setData({
        winHeight: rows * 100 + 600
      })
    }
    console.log("winHeight======" + rows * 80)
  },



  getData: function (e) {
    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "weekOrder": this.data.week,
      "year": this.data.year,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileType": 1,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_cookbook"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);

        that.setData({
          status: res.data.status
        })

        if (res.data.status == 0 || res.data.status == -1) {
          var content = res.data.data;
          var mealset = res.data.mealset
          var type = 1
          if (res.data.status == 0) {
            type = res.data.type
            if (type == 1) {

              var rows = 0;
              if (content.length > 0) {
                for (var i = 0; i < content.length; i++) {
                  var length = 0
                  for (var j = 0; j < content[i].mdata.length; j++) {
                    length += content[i].mdata[j].meal_list.length
                  }
                  if (length > rows) {
                    rows = length
                  }
                }
                if (rows > 0) {
                  that.setData({
                    winHeight: rows * 100 + 600
                  })
                }
              }


              var zhouyi = content[0].mdata
              var zhouer = content[1].mdata
              var zhousan = content[2].mdata
              var zhousi = content[3].mdata
              var zhouwu = content[4].mdata
              var zhouliu = content[5].mdata
              var zhouqi = content[6].mdata
              for (var i = 0; i < mealset.length; i++) {
                mealset[i].zhouyilist = []
                mealset[i].zhouerlist = []
                mealset[i].zhousanlist = []
                mealset[i].zhousilist = []
                mealset[i].zhouwulist = []
                mealset[i].zhouliulist = []
                mealset[i].zhouqilist = []
              }
              for (var i = 0; i < mealset.length; i++) {

                for (var j = 0; j < zhouyi[i].meal_list.length; j++) {
                  var mealobj = zhouyi[i].meal_list[j]
                  mealset[i].zhouyilist.push(mealobj)
                }

                for (var j = 0; j < zhouer[i].meal_list.length; j++) {
                  var mealobj = zhouer[i].meal_list[j]
                  mealset[i].zhouerlist.push(mealobj)
                }

                for (var j = 0; j < zhousan[i].meal_list.length; j++) {
                  var mealobj = zhousan[i].meal_list[j]
                  mealset[i].zhousanlist.push(mealobj)
                }

                for (var j = 0; j < zhousi[i].meal_list.length; j++) {
                  var mealobj = zhousi[i].meal_list[j]
                  mealset[i].zhousilist.push(mealobj)
                }

                for (var j = 0; j < zhouwu[i].meal_list.length; j++) {
                  var mealobj = zhouwu[i].meal_list[j]
                  mealset[i].zhouwulist.push(mealobj)
                }

                for (var j = 0; j < zhouliu[i].meal_list.length; j++) {
                  var mealobj = zhouliu[i].meal_list[j]
                  mealset[i].zhouliulist.push(mealobj)
                }

                for (var j = 0; j < zhouqi[i].meal_list.length; j++) {
                  var mealobj = zhouqi[i].meal_list[j]
                  mealset[i].zhouqilist.push(mealobj)
                }


              }
            } else {
              for (var i = 0; i < mealset.length; i++) {
                mealset[i].zhouyilist = []
                mealset[i].zhouerlist = []
                mealset[i].zhousanlist = []
                mealset[i].zhousilist = []
                mealset[i].zhouwulist = []
                mealset[i].zhouliulist = []
                mealset[i].zhouqilist = []
              }
              var photo = content[0].mdata[0].attachmentUrl
              var photolist = [photo]
              that.setData({
                photo: photo,
                photoList: photolist,
                saverurl: photo
              })
            }


          } else {
            for (var i = 0; i < mealset.length; i++) {
              mealset[i].zhouyilist = []
              mealset[i].zhouerlist = []
              mealset[i].zhousanlist = []
              mealset[i].zhousilist = []
              mealset[i].zhouwulist = []
              mealset[i].zhouliulist = []
              mealset[i].zhouqilist = []
            }
          }


          that.setData({
            content: content,
            leftDay: res.data.leftDay,
            rightDay: res.data.rightDay,
            cookbookaddrole: res.data.cookbookaddrole,
            type: type,
            dishes: res.data.dishes,
            mealset: mealset
          })
        }

      },
      fail: function (res) {},
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})